package com.mrobuy.jdvc.dao.impl;

import com.mrobuy.jdvc.dao.ItemDao;
import com.mrobuy.jdvc.dao.TaskDao;
import com.mrobuy.jdvc.entity.Item;
import com.mrobuy.jdvc.entity.Task;
import com.mrobuy.util.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Repository("itemDao")
public class ItemDaoImpl implements ItemDao {

    private static Logger LOG = LoggerFactory.getLogger(ItemDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Item> list() {

        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from ITEM where ZT = 'Y'");
        try {
            List<Item> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(Task.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public Pagination load(int start, int length, Item condition) {
        Pagination pagination = new Pagination();

        List<Object> params = new ArrayList<>();

        StringBuilder whereCond = new StringBuilder(" where ZT = 'Y' ");

        if ( condition.getName()!=null && condition.getName().trim().length() > 0 ) {
            whereCond.append("  and NAME like ? ");
            params.add("%" + condition.getName().trim() + "%");
        }

        if ( condition.getBrandname()!=null && condition.getBrandname().trim().length() > 0 ) {
            whereCond.append(" and BRANDNAME = ? ");
            params.add(condition.getBrandname().trim() );
        }
        if ( condition.getCategoryname()!=null && condition.getCategoryname().trim().length() > 0 ) {
            whereCond.append(" and CATEGORYNAME = ? ");
            params.add(condition.getCategoryname().trim() );
        }

        if ( condition.getType()!=null && condition.getType().length() > 0 ) {
            whereCond.append(" and TYPE = ? ");
            params.add(condition.getType().trim() );
        }

        if ( condition.getProductcode()!=null && condition.getProductcode().length() > 0 ) {
            whereCond.append(" and PRODUCTCODE = ? ");
            params.add(condition.getProductcode().trim() );
        }

        try {

            int count = jdbcTemplate.queryForObject("select count(*) from ITEM " + whereCond.toString(), params.toArray(new Object[params.size()]), Integer.class);

            params.add(start);
            params.add(length);
            List<Item> list = jdbcTemplate.query("select * from ITEM " + whereCond.toString() + " limit ?, ?", params.toArray(new Object[params.size()]), new BeanPropertyRowMapper(Item.class));


            pagination.setRecordsTotal(count);
            pagination.setRecordsFiltered(count);
            pagination.setData(list);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return pagination;
    }

    @Override
    public Item load(String code) {
        try {
            Item item = (Item)jdbcTemplate.queryForObject("select * from ITEM where CODE = ?", new Object[]{code}, new BeanPropertyRowMapper(Item.class));
            return item;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }

    @Override
    public List<Item> queryItems(String productcode, String type) {
        try {
            return jdbcTemplate.query("select * from ITEM where PRODUCTCODE = ? and TYPE = ? ", new Object[]{productcode, type}, new BeanPropertyRowMapper(Item.class));
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public Item queryItem(String code, String type) {
        try {
            Item item = (Item)jdbcTemplate.queryForObject("select * from ITEM where CODE = ? and TYPE = ? ", new Object[]{code, type}, new BeanPropertyRowMapper(Item.class));
            return item;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }
}
